import { create } from 'zustand'
import {
  mockDepartments,
  mockTracks,
  mockUsers,
  mockRoles,
  mockAccesses,
  mockLocks,
  mockLockRecords,
} from '@/mocks/admin-data'
import { mockTicketTemplates } from '@/mocks/ticket-templates'
import {
  User,
  Role,
  Department,
  Access,
  Track,
  Lock,
  LockRecord,
} from "@/types/admin"
import { TicketTemplate } from '@/types/template'
import { v4 as uuidv4 } from 'uuid'

interface AdminStore {
  // 部门状态管理
  departments: Department[]
  addDepartment: (department: Omit<Department, 'id'>) => Department
  updateDepartment: (id: string, department: Partial<Department>) => Department | null
  deleteDepartment: (id: string) => boolean

  // 股道状态管理
  tracks: Track[]
  addTrack: (track: Omit<Track, 'id'>) => Track
  updateTrack: (id: string, track: Partial<Track>) => Track | null
  deleteTrack: (id: string) => boolean

  // 用户状态管理
  users: User[]
  roles: Role[]
  addUser: (user: Omit<User, 'id'>) => User
  updateUser: (id: string, user: Partial<User>) => User | null
  deleteUser: (id: string) => boolean

  // 角色状态管理
  addRole: (role: Omit<Role, 'id'>) => Role
  updateRole: (id: string, role: Partial<Role>) => Role | null
  deleteRole: (id: string) => boolean

  // 门禁状态管理
  accesses: Access[]
  addAccess: (access: Omit<Access, 'id'>) => Access
  updateAccess: (id: string, access: Partial<Access>) => Access | null
  deleteAccess: (id: string) => boolean

  // 操作票模板状态管理
  templates: TicketTemplate[]
  addTemplate: (template: Omit<TicketTemplate, 'id'>) => TicketTemplate
  updateTemplate: (id: string, template: Partial<TicketTemplate>) => TicketTemplate | null
  deleteTemplate: (id: string) => boolean

  // 锁状态管理
  locks: Lock[]
  addLock: (lock: Lock) => void
  updateLock: (id: string, lock: Partial<Lock>) => void
  deleteLock: (id: string) => void

  // 锁记录状态管理
  lockRecords: LockRecord[]
  addLockRecord: (lockRecord: Omit<LockRecord, 'id'>) => LockRecord
  updateLockRecord: (id: string, lockRecord: Partial<LockRecord>) => LockRecord | null
  deleteLockRecord: (id: string) => boolean
}

export const useAdminStore = create<AdminStore>((set, get) => ({
  // 部门状态及操作
  departments: [...mockDepartments],

  addDepartment: (departmentData) => {
    const newDepartment: Department = {
      id: `dept-${uuidv4().substring(0, 8)}`,
      ...departmentData
    }

    set(state => ({
      departments: [...state.departments, newDepartment]
    }))

    return newDepartment
  },

  updateDepartment: (id, departmentData) => {
    let updatedDepartment: Department | null = null

    set(state => {
      const departments = state.departments.map(dept => {
        if (dept.id === id) {
          updatedDepartment = { ...dept, ...departmentData }
          return updatedDepartment
        }
        return dept
      })

      return { departments }
    })

    return updatedDepartment
  },

  deleteDepartment: (id) => {
    let success = false

    set(state => {
      const departments = state.departments.filter(dept => {
        if (dept.id === id) {
          success = true
          return false
        }
        return true
      })

      return { departments }
    })

    return success
  },

  // 股道状态及操作
  tracks: [...mockTracks],

  addTrack: (trackData) => {
    const newTrack: Track = {
      id: `track-${uuidv4().substring(0, 8)}`,
      ...trackData
    }

    set(state => ({
      tracks: [...state.tracks, newTrack]
    }))

    return newTrack
  },

  updateTrack: (id, trackData) => {
    let updatedTrack: Track | null = null

    set(state => {
      const tracks = state.tracks.map(track => {
        if (track.id === id) {
          updatedTrack = { ...track, ...trackData }
          return updatedTrack
        }
        return track
      })

      return { tracks }
    })

    return updatedTrack
  },

  deleteTrack: (id) => {
    let success = false

    set(state => {
      const tracks = state.tracks.filter(track => {
        if (track.id === id) {
          success = true
          return false
        }
        return true
      })

      return { tracks }
    })

    return success
  },

  // 用户状态及操作
  users: [...mockUsers],
  roles: [...mockRoles],

  addUser: (userData) => {
    const newUser: User = {
      id: `user-${uuidv4().substring(0, 8)}`,
      ...userData
    }

    set(state => ({
      users: [...state.users, newUser]
    }))

    return newUser
  },

  updateUser: (id, userData) => {
    let updatedUser: User | null = null

    set(state => {
      const users = state.users.map(user => {
        if (user.id === id) {
          updatedUser = { ...user, ...userData }
          return updatedUser
        }
        return user
      })

      return { users }
    })

    return updatedUser
  },

  deleteUser: (id) => {
    let success = false

    set(state => {
      const users = state.users.filter(user => {
        if (user.id === id) {
          success = true
          return false
        }
        return true
      })

      return { users }
    })

    return success
  },

  // 角色状态及操作
  addRole: (roleData) => {
    const newRole: Role = {
      id: `role-${uuidv4().substring(0, 8)}`,
      ...roleData
    }

    set(state => ({
      roles: [...state.roles, newRole]
    }))

    return newRole
  },

  updateRole: (id, roleData) => {
    let updatedRole: Role | null = null

    set(state => {
      const roles = state.roles.map(role => {
        if (role.id === id) {
          updatedRole = { ...role, ...roleData }
          return updatedRole
        }
        return role
      })

      return { roles }
    })

    return updatedRole
  },

  deleteRole: (id) => {
    let success = false

    set(state => {
      const roles = state.roles.filter(role => {
        if (role.id === id) {
          success = true
          return false
        }
        return true
      })

      return { roles }
    })

    return success
  },

  // 门禁状态及操作
  accesses: [...mockAccesses],

  addAccess: (accessData) => {
    const newAccess: Access = {
      id: `access-${uuidv4().substring(0, 8)}`,
      ...accessData
    }

    set(state => ({
      accesses: [...state.accesses, newAccess]
    }))

    return newAccess
  },

  updateAccess: (id, accessData) => {
    let updatedAccess: Access | null = null

    set(state => {
      const accesses = state.accesses.map(access => {
        if (access.id === id) {
          updatedAccess = { ...access, ...accessData }
          return updatedAccess
        }
        return access
      })

      return { accesses }
    })

    return updatedAccess
  },

  deleteAccess: (id) => {
    let success = false

    set(state => {
      const accesses = state.accesses.filter(access => {
        if (access.id === id) {
          success = true
          return false
        }
        return true
      })

      return { accesses }
    })

    return success
  },

  // 操作票模板状态及操作
  templates: [...mockTicketTemplates],

  addTemplate: (templateData) => {
    const newTemplate: TicketTemplate = {
      id: `template-${uuidv4().substring(0, 8)}`,
      ...templateData
    }

    set(state => ({
      templates: [...state.templates, newTemplate]
    }))

    return newTemplate
  },

  updateTemplate: (id, templateData) => {
    let updatedTemplate: TicketTemplate | null = null

    set(state => {
      const templates = state.templates.map(template => {
        if (template.id === id) {
          updatedTemplate = { ...template, ...templateData }
          return updatedTemplate
        }
        return template
      })

      return { templates }
    })

    return updatedTemplate
  },

  deleteTemplate: (id) => {
    let success = false

    set(state => {
      const templates = state.templates.filter(template => {
        if (template.id === id) {
          success = true
          return false
        }
        return true
      })

      return { templates }
    })

    return success
  },

  // 锁状态及操作
  locks: [...mockLocks],
  addLock: (lock) => set((state) => ({ locks: [...state.locks, lock] })),
  updateLock: (id, lock) =>
    set((state) => ({
      locks: state.locks.map((l) => (l.id === id ? { ...l, ...lock } : l)),
    })),
  deleteLock: (id) =>
    set((state) => ({
      locks: state.locks.filter((l) => l.id !== id),
    })),

  // 锁记录状态及操作
  lockRecords: [...mockLockRecords],
  addLockRecord: (lockRecordData) => {
    const newLockRecord: LockRecord = {
      id: `lock-record-${uuidv4().substring(0, 8)}`,
      ...lockRecordData
    }

    set(state => ({
      lockRecords: [...state.lockRecords, newLockRecord]
    }))

    return newLockRecord
  },
  updateLockRecord: (id, lockRecordData) => {
    let updatedLockRecord: LockRecord | null = null

    set(state => {
      const lockRecords = state.lockRecords.map(lockRecord => {
        if (lockRecord.id === id) {
          updatedLockRecord = { ...lockRecord, ...lockRecordData }
          return updatedLockRecord
        }
        return lockRecord
      })

      return { lockRecords }
    })

    return updatedLockRecord
  },
  deleteLockRecord: (id) => {
    let success = false

    set(state => {
      const lockRecords = state.lockRecords.filter(lockRecord => {
        if (lockRecord.id === id) {
          success = true
          return false
        }
        return true
      })

      return { lockRecords }
    })

    return success
  }
})) 